ggplot(diamonds, aes(x=cut)) +
geom_bar()
ggplot(diamonds, aes(x=cut)) +
geom_bar() +
coord_flip()
ggplot(diamonds, aes(x=cut, colour = color, fill = color)) +
geom_bar()
ggplot(diamonds, aes(x=cut, colour = color, fill = color)) +
geom_bar(position = "dodge")
ggplot(mtcars, aes(x = hp, y = mpg)) +
geom_point()
ggplot(mtcars, aes(x = hp, y = mpg)) +
geom_point() +
geom_smooth()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
ggplot(mtcars, aes(x = hp, y = mpg)) +
geom_point() +
geom_smooth(color = "red", method = "lm")
ggplot(iris, aes(x = Petal.Length, fill = Species)) +
geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
ggplot(iris, aes(x = Sepal.Length)) +
geom_density(fill = "green", alpha = .6)
ggplot(iris, aes(x = Sepal.Length, fill = Species)) +
geom_density(alpha = .6)
ggplot(starwars, aes(x = species, y = height)) +
geom_boxplot()
## Warning: Removed 6 rows containing non-finite values (stat_boxplot).
ggplot(starwars, aes(x = gender, y = mass, fill = gender)) +
geom_violin()
## Warning: Removed 28 rows containing non-finite values (stat_ydensity).
ggplot(starwars, aes(x = gender, y = mass, fill = gender)) +
geom_violin(draw_quantiles = c(.25, .5, .75))
## Warning: Removed 28 rows containing non-finite values (stat_ydensity).
ggplot(airquality, aes(x=Date, y = Wind)) +
geom_line() +
geom_point()
## Multiple Time Series
ggplot(airquality, aes(x=Date)) +
geom_line(aes(y = Wind), color = "blue") +
geom_line(aes(y = Temp), color = "red")
## Alternative
m.aq <- melt(airquality, id = "Date") %>%
filter(variable %in% c("Wind", "Temp"))
ggplot(m.aq, aes(x=Date, y=value, color = variable)) +
geom_point() +
geom_line()
ggplot(m.aq, aes(x=Date, y=value, color = variable)) +
geom_point() +
geom_line() +
xlab("Date") +
ylab("Wind: mph / Temp: degrees F") +
labs(color = "Measurement", caption = "Source: National Weather Service") +
ggtitle("Observed Wind and Temperature", subtitle = "New York City 1973")
ggplot(starwars, aes(x = gender, y = mass, fill = gender)) +
geom_violin(draw_quantiles = c(.25, .5, .75)) +
scale_fill_brewer(palette = "Set1")
## Warning: Removed 28 rows containing non-finite values (stat_ydensity).
# Customt ggplot theme
custom_theme <- theme(
line = element_line(colour = "black", size = 0.5,
linetype = 1, lineend = "butt"),
rect = element_rect(fill = "white", colour = "black",
size = 0.5, linetype = 1),
text = element_text(family = "", face = "plain",
colour = "black", size = 11,
lineheight = 0.9, hjust = 0.5,
vjust = 0.5, angle = 0,
margin = margin(), debug = FALSE),
axis.line = element_blank(),
axis.text = element_text(size = rel(0.8), colour = "grey30"),
axis.text.x = element_text(margin = margin(t = 0.8*5.5/2),
vjust = 1),
axis.text.y = element_text(margin = margin(r = 0.8*5.5/2),
hjust = 1),
axis.ticks = element_line(colour = "grey20"),
axis.ticks.length = unit(5.5/2, "pt"),
axis.title.x = element_text(margin = margin(t = 0.8 * 5.5,
b = 0.8 * 5.5/2)),
axis.title.y = element_text(angle = 90,
margin = margin(r = 0.8 * 5.5,
l = 0.8 * 5.5/2)),
legend.background = element_rect(colour = NA),
legend.spacing = unit(0.2, "cm"),
legend.key = element_rect(fill = "grey95", colour = "white"),
legend.key.size = unit(1.2, "lines"),
legend.key.height = NULL,
legend.key.width = NULL,
legend.text = element_text(size = rel(0.8)),
legend.text.align = NULL,
legend.title = element_text(hjust = 0),
legend.title.align = NULL,
legend.position = "right",
legend.direction = NULL,
legend.justification = "center",
legend.box = NULL,
panel.background = element_rect(fill = "white"),
panel.grid.major = element_line(colour = "grey", size=0.5, linetype="dashed"),
panel.border = element_rect(fill=NA, color="grey", size=0.5, linetype="solid"),
panel.grid.minor = element_line(size = 0.25, linetype = 'solid', colour = "white"),
panel.spacing = unit(5.5, "pt"), panel.margin.x = NULL,
panel.spacing.y = NULL, panel.ontop = FALSE,
strip.background = element_rect(fill = "grey85", colour = NA),
strip.text = element_text(colour = "grey10", size = rel(0.8)),
strip.text.x = element_text(margin = margin(t = 5.5,
b = 5.5)),
strip.text.y = element_text(angle = -90,
margin = margin(l = 5.5,
r = 5.5)),
strip.switch.pad.grid = unit(0.1, "cm"),
strip.switch.pad.wrap = unit(0.1, "cm"),
plot.background = element_rect(colour = "white"),
plot.title = element_text(size = rel(1.2),
margin = margin(b = 5.5 * 1.2)),
plot.margin = margin(5.5, 5.5, 5.5, 5.5),
complete = TRUE)
# The Rest of our plots will use this theme
theme_set(custom_theme)
## Warning: New theme missing the following elements: axis.title.x.top,
## axis.title.y.right, axis.text.x.top, axis.text.y.right, axis.line.x,
## axis.line.y, legend.margin, legend.spacing.x, legend.spacing.y,
## legend.box.margin, legend.box.background, legend.box.spacing,
## panel.spacing.x, panel.grid, plot.subtitle, plot.caption, plot.tag,
## plot.tag.position, strip.placement
ggplot(m.aq, aes(x=Date, y=value, color = variable)) +
geom_point() +
geom_line() +
scale_color_brewer(palette = "Set1") +
xlab("Date") +
ylab("Wind: mph / Temp: degrees F") +
labs(color = "Measurement", caption = "Source: National Weather Service") +
ggtitle("Observed Wind and Temperature", subtitle = "New York City 1973")
ggplot(starwars, aes(x = species, y = height, fill = species)) +
geom_boxplot() +
theme(axis.text.x = element_text(angle = 60, hjust = 1)) +
guides(fill = FALSE) +
xlab("Species") +
ylab("Height") +
ggtitle("Distribution of Height by Species", subtitle = "of Star Wars Characters")
## Warning: Removed 6 rows containing non-finite values (stat_boxplot).
ggplot(iris, aes(x = Sepal.Length, fill = Species)) +
geom_density(alpha = .6) +
scale_fill_brewer(palette = "Dark2") +
facet_wrap("Species") +
xlab("Sepal Length") +
ylab("Density") +
ggtitle("Density of Sepal Length by Species")
guides(fill = FALSE)
## $fill
## [1] FALSE
##
## attr(,"class")
## [1] "guides"
ggplot(mtcars, aes(x = hp, y = mpg)) +
geom_point() +
geom_hline(yintercept = mean(mtcars$mpg), color = "red") +
ggtitle("Car Miles per Gallon by Horse Power") +
xlab("Horse Power") +
ylab("MPG")
ggplot(mtcars, aes(x = hp, y = mpg)) +
geom_point() +
geom_vline(xintercept = mean(mtcars$hp), color = "blue") +
ggtitle("Car Miles per Gallon by Horse Power") +
xlab("Horse Power") +
ylab("MPG")
ggplot(mtcars, aes(x = hp, y = mpg)) +
geom_point() +
geom_text(aes(x = 0, y = mean(mtcars$mpg), label = "MPG Average"), color = "red", vjust = 0, hjust = 0) +
geom_hline(yintercept = mean(mtcars$mpg), color = "red") +
geom_text(aes(x = mean(mtcars$hp), y = 0, label = "HP Average"), color = "blue", angle = 90, vjust = 0, hjust = 0) +
geom_vline(xintercept = mean(mtcars$hp), color = "blue") +
ggtitle("Car Miles per Gallon by Horse Power") +
xlab("Horse Power") +
ylab("MPG")
lines <- mtcars %>%
summarise(mpg = mean(mpg), hp = mean(hp))
ggplot(mtcars, aes(x = hp, y = mpg)) +
geom_point() +
geom_hline(data = lines, aes(yintercept = mpg, color = "MPG"), show.legend = T) +
geom_vline(data = lines, aes(xintercept = hp, color = "HP"), show.legend = T) +
ggtitle("Car Miles per Gallon by Horse Power") +
xlab("Horse Power") +
ylab("MPG") +
labs(color = "Averages")
ggplot(mtcars, aes(x = hp, y = mpg)) +
geom_point() +
geom_text(aes(label = rowname), angle = 45, hjust = 0, vjust = 1, nudge_y = .5)
ggplotly(ggplot(mtcars, aes(x = hp, y = mpg)) +
geom_point() +
geom_smooth() +
ggtitle("Car Miles per Gallon by Horse Power") +
xlab("Horse Power") +
ylab("MPG")
)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
# Text will format HTML automatically
ggplotly(ggplot(mtcars, aes(x = hp, y = mpg, text = paste("<b>", rowname, "</b><br>",
"MPG:", mpg,
"<br>Horse Power:", hp))) +
geom_point() +
ggtitle("Car Miles per Gallon by Horse Power") +
xlab("Horse Power") +
ylab("MPG"),
tooltip = "text"
)